home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / generic / tkMenubutton.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  26.8 KB  |  866 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkMenubutton.c --
  3.  *
  4.  *    This module implements button-like widgets that are used
  5.  *    to invoke pull-down menus.
  6.  *
  7.  * Copyright (c) 1990-1994 The Regents of the University of California.
  8.  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
  9.  *
  10.  * See the file "license.terms" for information on usage and redistribution
  11.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12.  *
  13.  * SCCS: @(#) tkMenubutton.c 1.94 97/07/31 09:10:37
  14.  */
  15.  
  16. #include "tkMenubutton.h"
  17. #include "tkPort.h"
  18. #include "default.h"
  19.  
  20. /*
  21.  * Uids internal to menubuttons.
  22.  */
  23.  
  24. static Tk_Uid aboveUid = NULL;
  25. static Tk_Uid belowUid = NULL;
  26. static Tk_Uid leftUid = NULL;
  27. static Tk_Uid rightUid = NULL;
  28. static Tk_Uid flushUid = NULL;
  29.  
  30. /*
  31.  * Information used for parsing configuration specs:
  32.  */
  33.  
  34. static Tk_ConfigSpec configSpecs[] = {
  35.     {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
  36.     DEF_MENUBUTTON_ACTIVE_BG_COLOR, Tk_Offset(TkMenuButton, activeBorder),
  37.     TK_CONFIG_COLOR_ONLY},
  38.     {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
  39.     DEF_MENUBUTTON_ACTIVE_BG_MONO, Tk_Offset(TkMenuButton, activeBorder),
  40.     TK_CONFIG_MONO_ONLY},
  41.     {TK_CONFIG_COLOR, "-activeforeground", "activeForeground", "Background",
  42.     DEF_MENUBUTTON_ACTIVE_FG_COLOR, Tk_Offset(TkMenuButton, activeFg),
  43.     TK_CONFIG_COLOR_ONLY},
  44.     {TK_CONFIG_COLOR, "-activeforeground", "activeForeground", "Background",
  45.     DEF_MENUBUTTON_ACTIVE_FG_MONO, Tk_Offset(TkMenuButton, activeFg),
  46.     TK_CONFIG_MONO_ONLY},
  47.     {TK_CONFIG_ANCHOR, "-anchor", "anchor", "Anchor",
  48.     DEF_MENUBUTTON_ANCHOR, Tk_Offset(TkMenuButton, anchor), 0},
  49.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  50.     DEF_MENUBUTTON_BG_COLOR, Tk_Offset(TkMenuButton, normalBorder),
  51.     TK_CONFIG_COLOR_ONLY},
  52.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  53.     DEF_MENUBUTTON_BG_MONO, Tk_Offset(TkMenuButton, normalBorder),
  54.     TK_CONFIG_MONO_ONLY},
  55.     {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
  56.     (char *) NULL, 0, 0},
  57.     {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
  58.     (char *) NULL, 0, 0},
  59.     {TK_CONFIG_BITMAP, "-bitmap", "bitmap", "Bitmap",
  60.     DEF_MENUBUTTON_BITMAP, Tk_Offset(TkMenuButton, bitmap),
  61.     TK_CONFIG_NULL_OK},
  62.     {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
  63.     DEF_MENUBUTTON_BORDER_WIDTH, Tk_Offset(TkMenuButton, borderWidth), 0},
  64.     {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
  65.     DEF_MENUBUTTON_CURSOR, Tk_Offset(TkMenuButton, cursor),
  66.     TK_CONFIG_NULL_OK},
  67.     {TK_CONFIG_UID, "-direction", "direction", "Direction",
  68.         DEF_MENUBUTTON_DIRECTION, Tk_Offset(TkMenuButton, direction), 
  69.     0},
  70.     {TK_CONFIG_COLOR, "-disabledforeground", "disabledForeground",
  71.     "DisabledForeground", DEF_MENUBUTTON_DISABLED_FG_COLOR,
  72.     Tk_Offset(TkMenuButton, disabledFg),
  73.     TK_CONFIG_COLOR_ONLY|TK_CONFIG_NULL_OK},
  74.     {TK_CONFIG_COLOR, "-disabledforeground", "disabledForeground",
  75.     "DisabledForeground", DEF_MENUBUTTON_DISABLED_FG_MONO,
  76.     Tk_Offset(TkMenuButton, disabledFg),
  77.     TK_CONFIG_MONO_ONLY|TK_CONFIG_NULL_OK},
  78.     {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
  79.     (char *) NULL, 0, 0},
  80.     {TK_CONFIG_FONT, "-font", "font", "Font",
  81.     DEF_MENUBUTTON_FONT, Tk_Offset(TkMenuButton, tkfont), 0},
  82.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  83.     DEF_MENUBUTTON_FG, Tk_Offset(TkMenuButton, normalFg), 0},
  84.     {TK_CONFIG_STRING, "-height", "height", "Height",
  85.     DEF_MENUBUTTON_HEIGHT, Tk_Offset(TkMenuButton, heightString), 0},
  86.     {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
  87.     "HighlightBackground", DEF_MENUBUTTON_HIGHLIGHT_BG,
  88.     Tk_Offset(TkMenuButton, highlightBgColorPtr), 0},
  89.     {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
  90.     DEF_MENUBUTTON_HIGHLIGHT, Tk_Offset(TkMenuButton, highlightColorPtr),
  91.     0},
  92.     {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
  93.     "HighlightThickness", DEF_MENUBUTTON_HIGHLIGHT_WIDTH,
  94.     Tk_Offset(TkMenuButton, highlightWidth), 0},
  95.     {TK_CONFIG_STRING, "-image", "image", "Image",
  96.     DEF_MENUBUTTON_IMAGE, Tk_Offset(TkMenuButton, imageString),
  97.     TK_CONFIG_NULL_OK},
  98.     {TK_CONFIG_BOOLEAN, "-indicatoron", "indicatorOn", "IndicatorOn",
  99.     DEF_MENUBUTTON_INDICATOR, Tk_Offset(TkMenuButton, indicatorOn), 0},
  100.     {TK_CONFIG_JUSTIFY, "-justify", "justify", "Justify",
  101.     DEF_MENUBUTTON_JUSTIFY, Tk_Offset(TkMenuButton, justify), 0},
  102.     {TK_CONFIG_STRING, "-menu", "menu", "Menu",
  103.     DEF_MENUBUTTON_MENU, Tk_Offset(TkMenuButton, menuName),
  104.     TK_CONFIG_NULL_OK},
  105.     {TK_CONFIG_PIXELS, "-padx", "padX", "Pad",
  106.     DEF_MENUBUTTON_PADX, Tk_Offset(TkMenuButton, padX), 0},
  107.     {TK_CONFIG_PIXELS, "-pady", "padY", "Pad",
  108.     DEF_MENUBUTTON_PADY, Tk_Offset(TkMenuButton, padY), 0},
  109.     {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
  110.     DEF_MENUBUTTON_RELIEF, Tk_Offset(TkMenuButton, relief), 0},
  111.     {TK_CONFIG_UID, "-state", "state", "State",
  112.     DEF_MENUBUTTON_STATE, Tk_Offset(TkMenuButton, state), 0},
  113.     {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
  114.     DEF_MENUBUTTON_TAKE_FOCUS, Tk_Offset(TkMenuButton, takeFocus),
  115.     TK_CONFIG_NULL_OK},
  116.     {TK_CONFIG_STRING, "-text", "text", "Text",
  117.     DEF_MENUBUTTON_TEXT, Tk_Offset(TkMenuButton, text), 0},
  118.     {TK_CONFIG_STRING, "-textvariable", "textVariable", "Variable",
  119.     DEF_MENUBUTTON_TEXT_VARIABLE, Tk_Offset(TkMenuButton, textVarName),
  120.     TK_CONFIG_NULL_OK},
  121.     {TK_CONFIG_INT, "-underline", "underline", "Underline",
  122.     DEF_MENUBUTTON_UNDERLINE, Tk_Offset(TkMenuButton, underline), 0},
  123.     {TK_CONFIG_STRING, "-width", "width", "Width",
  124.     DEF_MENUBUTTON_WIDTH, Tk_Offset(TkMenuButton, widthString), 0},
  125.     {TK_CONFIG_PIXELS, "-wraplength", "wrapLength", "WrapLength",
  126.     DEF_MENUBUTTON_WRAP_LENGTH, Tk_Offset(TkMenuButton, wrapLength), 0},
  127.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  128.     (char *) NULL, 0, 0}
  129. };
  130.  
  131. /*
  132.  * Forward declarations for procedures defined later in this file:
  133.  */
  134.  
  135. static void        MenuButtonCmdDeletedProc _ANSI_ARGS_((
  136.                 ClientData clientData));
  137. static void        MenuButtonEventProc _ANSI_ARGS_((ClientData clientData,
  138.                 XEvent *eventPtr));
  139. static void        MenuButtonImageProc _ANSI_ARGS_((ClientData clientData,
  140.                 int x, int y, int width, int height, int imgWidth,
  141.                 int imgHeight));
  142. static char *        MenuButtonTextVarProc _ANSI_ARGS_((
  143.                 ClientData clientData, Tcl_Interp *interp,
  144.                 char *name1, char *name2, int flags));
  145. static int        MenuButtonWidgetCmd _ANSI_ARGS_((ClientData clientData,
  146.                 Tcl_Interp *interp, int argc, char **argv));
  147. static int        ConfigureMenuButton _ANSI_ARGS_((Tcl_Interp *interp,
  148.                 TkMenuButton *mbPtr, int argc, char **argv,
  149.                 int flags));
  150. static void        DestroyMenuButton _ANSI_ARGS_((char *memPtr));
  151.  
  152. /*
  153.  *--------------------------------------------------------------
  154.  *
  155.  * Tk_MenubuttonCmd --
  156.  *
  157.  *    This procedure is invoked to process the "button", "label",
  158.  *    "radiobutton", and "checkbutton" Tcl commands.  See the
  159.  *    user documentation for details on what it does.
  160.  *
  161.  * Results:
  162.  *    A standard Tcl result.
  163.  *
  164.  * Side effects:
  165.  *    See the user documentation.
  166.  *
  167.  *--------------------------------------------------------------
  168.  */
  169.  
  170. int
  171. Tk_MenubuttonCmd(clientData, interp, argc, argv)
  172.     ClientData clientData;    /* Main window associated with
  173.                  * interpreter. */
  174.     Tcl_Interp *interp;        /* Current interpreter. */
  175.     int argc;            /* Number of arguments. */
  176.     char **argv;        /* Argument strings. */
  177. {
  178.     register TkMenuButton *mbPtr;
  179.     Tk_Window tkwin = (Tk_Window) clientData;
  180.     Tk_Window new;
  181.  
  182.     if (argc < 2) {
  183.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  184.         argv[0], " pathName ?options?\"", (char *) NULL);
  185.     return TCL_ERROR;
  186.     }
  187.  
  188.     /*
  189.      * Create the new window.
  190.      */
  191.  
  192.     new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
  193.     if (new == NULL) {
  194.     return TCL_ERROR;
  195.     }
  196.  
  197.     Tk_SetClass(new, "Menubutton");
  198.     mbPtr = TkpCreateMenuButton(new);
  199.  
  200.     TkSetClassProcs(new, &tkpMenubuttonClass, (ClientData) mbPtr);
  201.  
  202.     /*
  203.      * Initialize the data structure for the button.
  204.      */
  205.  
  206.     mbPtr->tkwin = new;
  207.     mbPtr->display = Tk_Display (new);
  208.     mbPtr->interp = interp;
  209.     mbPtr->widgetCmd = Tcl_CreateCommand(interp, Tk_PathName(mbPtr->tkwin),
  210.         MenuButtonWidgetCmd, (ClientData) mbPtr, MenuButtonCmdDeletedProc);
  211.     mbPtr->menuName = NULL;
  212.     mbPtr->text = NULL;
  213.     mbPtr->underline = -1;
  214.     mbPtr->textVarName = NULL;
  215.     mbPtr->bitmap = None;
  216.     mbPtr->imageString = NULL;
  217.     mbPtr->image = NULL;
  218.     mbPtr->state = tkNormalUid;
  219.     mbPtr->normalBorder = NULL;
  220.     mbPtr->activeBorder = NULL;
  221.     mbPtr->borderWidth = 0;
  222.     mbPtr->relief = TK_RELIEF_FLAT;
  223.     mbPtr->highlightWidth = 0;
  224.     mbPtr->highlightBgColorPtr = NULL;
  225.     mbPtr->highlightColorPtr = NULL;
  226.     mbPtr->inset = 0;
  227.     mbPtr->tkfont = NULL;
  228.     mbPtr->normalFg = NULL;
  229.     mbPtr->activeFg = NULL;
  230.     mbPtr->disabledFg = NULL;
  231.     mbPtr->normalTextGC = None;
  232.     mbPtr->activeTextGC = None;
  233.     mbPtr->gray = None;
  234.     mbPtr->disabledGC = None;
  235.     mbPtr->leftBearing = 0;
  236.     mbPtr->rightBearing = 0;
  237.     mbPtr->widthString = NULL;
  238.     mbPtr->heightString = NULL;
  239.     mbPtr->width = 0;
  240.     mbPtr->width = 0;
  241.     mbPtr->wrapLength = 0;
  242.     mbPtr->padX = 0;
  243.     mbPtr->padY = 0;
  244.     mbPtr->anchor = TK_ANCHOR_CENTER;
  245.     mbPtr->justify = TK_JUSTIFY_CENTER;
  246.     mbPtr->textLayout = NULL;
  247.     mbPtr->indicatorOn = 0;
  248.     mbPtr->indicatorWidth = 0;
  249.     mbPtr->indicatorHeight = 0;
  250.     mbPtr->cursor = None;
  251.     mbPtr->takeFocus = NULL;
  252.     mbPtr->flags = 0;
  253.     if (aboveUid == NULL) {
  254.     aboveUid = Tk_GetUid("above");
  255.     belowUid = Tk_GetUid("below");
  256.     leftUid = Tk_GetUid("left");
  257.     rightUid = Tk_GetUid("right");
  258.     flushUid = Tk_GetUid("flush");
  259.     }
  260.     mbPtr->direction = flushUid;
  261.  
  262.     Tk_CreateEventHandler(mbPtr->tkwin,
  263.         ExposureMask|StructureNotifyMask|FocusChangeMask,
  264.         MenuButtonEventProc, (ClientData) mbPtr);
  265.     if (ConfigureMenuButton(interp, mbPtr, argc-2, argv+2, 0) != TCL_OK) {
  266.     Tk_DestroyWindow(mbPtr->tkwin);
  267.     return TCL_ERROR;
  268.     }
  269.  
  270.     interp->result = Tk_PathName(mbPtr->tkwin);
  271.     return TCL_OK;
  272. }
  273.  
  274. /*
  275.  *--------------------------------------------------------------
  276.  *
  277.  * MenuButtonWidgetCmd --
  278.  *
  279.  *    This procedure is invoked to process the Tcl command
  280.  *    that corresponds to a widget managed by this module.
  281.  *    See the user documentation for details on what it does.
  282.  *
  283.  * Results:
  284.  *    A standard Tcl result.
  285.  *
  286.  * Side effects:
  287.  *    See the user documentation.
  288.  *
  289.  *--------------------------------------------------------------
  290.  */
  291.  
  292. static int
  293. MenuButtonWidgetCmd(clientData, interp, argc, argv)
  294.     ClientData clientData;    /* Information about button widget. */
  295.     Tcl_Interp *interp;        /* Current interpreter. */
  296.     int argc;            /* Number of arguments. */
  297.     char **argv;        /* Argument strings. */
  298. {
  299.     register TkMenuButton *mbPtr = (TkMenuButton *) clientData;
  300.     int result;
  301.     size_t length;
  302.     int c;
  303.  
  304.     if (argc < 2) {
  305.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  306.         " option ?arg arg ...?\"", (char *) NULL);
  307.     return TCL_ERROR;
  308.     }
  309.     Tcl_Preserve((ClientData) mbPtr);
  310.     c = argv[1][0];
  311.     length = strlen(argv[1]);
  312.     if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  313.         && (length >= 2)) {
  314.     if (argc != 3) {
  315.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  316.             argv[0], " cget option\"",
  317.             (char *) NULL);
  318.         result = TCL_ERROR;
  319.     } else {
  320.         result = Tk_ConfigureValue(interp, mbPtr->tkwin, configSpecs,
  321.             (char *) mbPtr, argv[2], 0);
  322.     }
  323.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  324.         && (length >= 2)) {
  325.     if (argc == 2) {
  326.         result = Tk_ConfigureInfo(interp, mbPtr->tkwin, configSpecs,
  327.             (char *) mbPtr, (char *) NULL, 0);
  328.     } else if (argc == 3) {
  329.         result = Tk_ConfigureInfo(interp, mbPtr->tkwin, configSpecs,
  330.             (char *) mbPtr, argv[2], 0);
  331.     } else {
  332.         result = ConfigureMenuButton(interp, mbPtr, argc-2, argv+2,
  333.             TK_CONFIG_ARGV_ONLY);
  334.     }
  335.     } else {
  336.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  337.         "\": must be cget or configure",
  338.         (char *) NULL);
  339.     result = TCL_ERROR;
  340.     }
  341.     Tcl_Release((ClientData) mbPtr);
  342.     return result;
  343. }
  344.  
  345. /*
  346.  *----------------------------------------------------------------------
  347.  *
  348.  * DestroyMenuButton --
  349.  *
  350.  *    This procedure is invoked to recycle all of the resources
  351.  *    associated with a button widget.  It is invoked as a
  352.  *    when-idle handler in order to make sure that there is no
  353.  *    other use of the button pending at the time of the deletion.
  354.  *
  355.  * Results:
  356.  *    None.
  357.  *
  358.  * Side effects:
  359.  *    Everything associated with the widget is freed up.
  360.  *
  361.  *----------------------------------------------------------------------
  362.  */
  363.  
  364. static void
  365. DestroyMenuButton(memPtr)
  366.     char *memPtr;        /* Info about button widget. */
  367. {
  368.     register TkMenuButton *mbPtr = (TkMenuButton *) memPtr;
  369.  
  370.     /*
  371.      * Free up all the stuff that requires special handling, then
  372.      * let Tk_FreeOptions handle all the standard option-related
  373.      * stuff.
  374.      */
  375.  
  376.     if (mbPtr->textVarName != NULL) {
  377.     Tcl_UntraceVar(mbPtr->interp, mbPtr->textVarName,
  378.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  379.         MenuButtonTextVarProc, (ClientData) mbPtr);
  380.     }
  381.     if (mbPtr->image != NULL) {
  382.     Tk_FreeImage(mbPtr->image);
  383.     }
  384.     if (mbPtr->normalTextGC != None) {
  385.     Tk_FreeGC(mbPtr->display, mbPtr->normalTextGC);
  386.     }
  387.     if (mbPtr->activeTextGC != None) {
  388.     Tk_FreeGC(mbPtr->display, mbPtr->activeTextGC);
  389.     }
  390.     if (mbPtr->gray != None) {
  391.     Tk_FreeBitmap(mbPtr->display, mbPtr->gray);
  392.     }
  393.     if (mbPtr->disabledGC != None) {
  394.     Tk_FreeGC(mbPtr->display, mbPtr->disabledGC);
  395.     }
  396.     Tk_FreeTextLayout(mbPtr->textLayout);
  397.     Tk_FreeOptions(configSpecs, (char *) mbPtr, mbPtr->display, 0);
  398.     ckfree((char *) mbPtr);
  399. }
  400.  
  401. /*
  402.  *----------------------------------------------------------------------
  403.  *
  404.  * ConfigureMenuButton --
  405.  *
  406.  *    This procedure is called to process an argv/argc list, plus
  407.  *    the Tk option database, in order to configure (or
  408.  *    reconfigure) a menubutton widget.
  409.  *
  410.  * Results:
  411.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  412.  *    returned, then interp->result contains an error message.
  413.  *
  414.  * Side effects:
  415.  *    Configuration information, such as text string, colors, font,
  416.  *    etc. get set for mbPtr;  old resources get freed, if there
  417.  *    were any.  The menubutton is redisplayed.
  418.  *
  419.  *----------------------------------------------------------------------
  420.  */
  421.  
  422. static int
  423. ConfigureMenuButton(interp, mbPtr, argc, argv, flags)
  424.     Tcl_Interp *interp;        /* Used for error reporting. */
  425.     register TkMenuButton *mbPtr;    /* Information about widget;  may or may
  426.                  * not already have values for some fields. */
  427.     int argc;            /* Number of valid entries in argv. */
  428.     char **argv;        /* Arguments. */
  429.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  430. {
  431.     int result;
  432.     Tk_Image image;
  433.  
  434.     /*
  435.      * Eliminate any existing trace on variables monitored by the menubutton.
  436.      */
  437.  
  438.     if (mbPtr->textVarName != NULL) {
  439.     Tcl_UntraceVar(interp, mbPtr->textVarName,
  440.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  441.         MenuButtonTextVarProc, (ClientData) mbPtr);
  442.     }
  443.  
  444.     result = Tk_ConfigureWidget(interp, mbPtr->tkwin, configSpecs,
  445.         argc, argv, (char *) mbPtr, flags);
  446.     if (result != TCL_OK) {
  447.     return TCL_ERROR;
  448.     }
  449.  
  450.     /*
  451.      * A few options need special processing, such as setting the
  452.      * background from a 3-D border, or filling in complicated
  453.      * defaults that couldn't be specified to Tk_ConfigureWidget.
  454.      */
  455.  
  456.     if ((mbPtr->state == tkActiveUid) && !Tk_StrictMotif(mbPtr->tkwin)) {
  457.     Tk_SetBackgroundFromBorder(mbPtr->tkwin, mbPtr->activeBorder);
  458.     } else {
  459.     Tk_SetBackgroundFromBorder(mbPtr->tkwin, mbPtr->normalBorder);
  460.     if ((mbPtr->state != tkNormalUid) && (mbPtr->state != tkActiveUid)
  461.         && (mbPtr->state != tkDisabledUid)) {
  462.         Tcl_AppendResult(interp, "bad state value \"", mbPtr->state,
  463.             "\": must be normal, active, or disabled", (char *) NULL);
  464.         mbPtr->state = tkNormalUid;
  465.         return TCL_ERROR;
  466.     }
  467.     }
  468.  
  469.     if ((mbPtr->direction != aboveUid) && (mbPtr->direction != belowUid)
  470.         && (mbPtr->direction != leftUid) && (mbPtr->direction != rightUid)
  471.         && (mbPtr->direction != flushUid)) {
  472.     Tcl_AppendResult(interp, "bad direction value \"", mbPtr->direction,
  473.         "\": must be above, below, left, right, or flush",
  474.         (char *) NULL);
  475.     mbPtr->direction = belowUid;
  476.     return TCL_ERROR;
  477.     }
  478.     
  479.     if (mbPtr->highlightWidth < 0) {
  480.     mbPtr->highlightWidth = 0;
  481.     }
  482.  
  483.     if (mbPtr->padX < 0) {
  484.     mbPtr->padX = 0;
  485.     }
  486.     if (mbPtr->padY < 0) {
  487.     mbPtr->padY = 0;
  488.     }
  489.  
  490.     /*
  491.      * Get the image for the widget, if there is one.  Allocate the
  492.      * new image before freeing the old one, so that the reference
  493.      * count doesn't go to zero and cause image data to be discarded.
  494.      */
  495.  
  496.     if (mbPtr->imageString != NULL) {
  497.     image = Tk_GetImage(mbPtr->interp, mbPtr->tkwin,
  498.         mbPtr->imageString, MenuButtonImageProc, (ClientData) mbPtr);
  499.     if (image == NULL) {
  500.         return TCL_ERROR;
  501.     }
  502.     } else {
  503.     image = NULL;
  504.     }
  505.     if (mbPtr->image != NULL) {
  506.     Tk_FreeImage(mbPtr->image);
  507.     }
  508.     mbPtr->image = image;
  509.  
  510.     if ((mbPtr->image == NULL) && (mbPtr->bitmap == None)
  511.         && (mbPtr->textVarName != NULL)) {
  512.     /*
  513.      * The menubutton displays a variable.  Set up a trace to watch
  514.      * for any changes in it.
  515.      */
  516.  
  517.     char *value;
  518.  
  519.     value = Tcl_GetVar(interp, mbPtr->textVarName, TCL_GLOBAL_ONLY);
  520.     if (value == NULL) {
  521.         Tcl_SetVar(interp, mbPtr->textVarName, mbPtr->text,
  522.             TCL_GLOBAL_ONLY);
  523.     } else {
  524.         if (mbPtr->text != NULL) {
  525.         ckfree(mbPtr->text);
  526.         }
  527.         mbPtr->text = (char *) ckalloc((unsigned) (strlen(value) + 1));
  528.         strcpy(mbPtr->text, value);
  529.     }
  530.     Tcl_TraceVar(interp, mbPtr->textVarName,
  531.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  532.         MenuButtonTextVarProc, (ClientData) mbPtr);
  533.     }
  534.  
  535.     /*
  536.      * Recompute the geometry for the button.
  537.      */
  538.  
  539.     if ((mbPtr->bitmap != None) || (mbPtr->image != NULL)) {
  540.     if (Tk_GetPixels(interp, mbPtr->tkwin, mbPtr->widthString,
  541.         &mbPtr->width) != TCL_OK) {
  542.         widthError:
  543.         Tcl_AddErrorInfo(interp, "\n    (processing -width option)");
  544.         return TCL_ERROR;
  545.     }
  546.     if (Tk_GetPixels(interp, mbPtr->tkwin, mbPtr->heightString,
  547.         &mbPtr->height) != TCL_OK) {
  548.         heightError:
  549.         Tcl_AddErrorInfo(interp, "\n    (processing -height option)");
  550.         return TCL_ERROR;
  551.     }
  552.     } else {
  553.     if (Tcl_GetInt(interp, mbPtr->widthString, &mbPtr->width)
  554.         != TCL_OK) {
  555.         goto widthError;
  556.     }
  557.     if (Tcl_GetInt(interp, mbPtr->heightString, &mbPtr->height)
  558.         != TCL_OK) {
  559.         goto heightError;
  560.     }
  561.     }
  562.     TkMenuButtonWorldChanged((ClientData) mbPtr);
  563.     return TCL_OK;
  564. }
  565.  
  566. /*
  567.  *---------------------------------------------------------------------------
  568.  *
  569.  * TkMenuButtonWorldChanged --
  570.  *
  571.  *      This procedure is called when the world has changed in some
  572.  *      way and the widget needs to recompute all its graphics contexts
  573.  *    and determine its new geometry.
  574.  *
  575.  * Results:
  576.  *      None.
  577.  *
  578.  * Side effects:
  579.  *      TkMenuButton will be relayed out and redisplayed.
  580.  *
  581.  *---------------------------------------------------------------------------
  582.  */
  583.  
  584. void
  585. TkMenuButtonWorldChanged(instanceData)
  586.     ClientData instanceData;    /* Information about widget. */
  587. {
  588.     XGCValues gcValues;
  589.     GC gc;
  590.     unsigned long mask;
  591.     TkMenuButton *mbPtr;
  592.  
  593.     mbPtr = (TkMenuButton *) instanceData;
  594.  
  595.     gcValues.font = Tk_FontId(mbPtr->tkfont);
  596.     gcValues.foreground = mbPtr->normalFg->pixel;
  597.     gcValues.background = Tk_3DBorderColor(mbPtr->normalBorder)->pixel;
  598.  
  599.     /*
  600.      * Note: GraphicsExpose events are disabled in GC's because they're
  601.      * used to copy stuff from an off-screen pixmap onto the screen (we know
  602.      * that there's no problem with obscured areas).
  603.      */
  604.  
  605.     gcValues.graphics_exposures = False;
  606.     mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
  607.     gc = Tk_GetGC(mbPtr->tkwin, mask, &gcValues);
  608.     if (mbPtr->normalTextGC != None) {
  609.     Tk_FreeGC(mbPtr->display, mbPtr->normalTextGC);
  610.     }
  611.     mbPtr->normalTextGC = gc;
  612.  
  613.     gcValues.font = Tk_FontId(mbPtr->tkfont);
  614.     gcValues.foreground = mbPtr->activeFg->pixel;
  615.     gcValues.background = Tk_3DBorderColor(mbPtr->activeBorder)->pixel;
  616.     mask = GCForeground | GCBackground | GCFont;
  617.     gc = Tk_GetGC(mbPtr->tkwin, mask, &gcValues);
  618.     if (mbPtr->activeTextGC != None) {
  619.     Tk_FreeGC(mbPtr->display, mbPtr->activeTextGC);
  620.     }
  621.     mbPtr->activeTextGC = gc;
  622.  
  623.     gcValues.font = Tk_FontId(mbPtr->tkfont);
  624.     gcValues.background = Tk_3DBorderColor(mbPtr->normalBorder)->pixel;
  625.     if ((mbPtr->disabledFg != NULL) && (mbPtr->imageString == NULL)) {
  626.     gcValues.foreground = mbPtr->disabledFg->pixel;
  627.     mask = GCForeground | GCBackground | GCFont;
  628.     } else {
  629.     gcValues.foreground = gcValues.background;
  630.     mask = GCForeground;
  631.     if (mbPtr->gray == None) {
  632.         mbPtr->gray = Tk_GetBitmap(NULL, mbPtr->tkwin,
  633.             Tk_GetUid("gray50"));
  634.     }
  635.     if (mbPtr->gray != None) {
  636.         gcValues.fill_style = FillStippled;
  637.         gcValues.stipple = mbPtr->gray;
  638.         mask |= GCFillStyle | GCStipple;
  639.     }
  640.     }
  641.     gc = Tk_GetGC(mbPtr->tkwin, mask, &gcValues);
  642.     if (mbPtr->disabledGC != None) {
  643.     Tk_FreeGC(mbPtr->display, mbPtr->disabledGC);
  644.     }
  645.     mbPtr->disabledGC = gc;
  646.  
  647.     TkpComputeMenuButtonGeometry(mbPtr);
  648.  
  649.     /*
  650.      * Lastly, arrange for the button to be redisplayed.
  651.      */
  652.  
  653.     if (Tk_IsMapped(mbPtr->tkwin) && !(mbPtr->flags & REDRAW_PENDING)) {
  654.     Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
  655.     mbPtr->flags |= REDRAW_PENDING;
  656.     }
  657. }
  658.  
  659. /*
  660.  *--------------------------------------------------------------
  661.  *
  662.  * MenuButtonEventProc --
  663.  *
  664.  *    This procedure is invoked by the Tk dispatcher for various
  665.  *    events on buttons.
  666.  *
  667.  * Results:
  668.  *    None.
  669.  *
  670.  * Side effects:
  671.  *    When the window gets deleted, internal structures get
  672.  *    cleaned up.  When it gets exposed, it is redisplayed.
  673.  *
  674.  *--------------------------------------------------------------
  675.  */
  676.  
  677. static void
  678. MenuButtonEventProc(clientData, eventPtr)
  679.     ClientData clientData;    /* Information about window. */
  680.     XEvent *eventPtr;        /* Information about event. */
  681. {
  682.     TkMenuButton *mbPtr = (TkMenuButton *) clientData;
  683.     if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
  684.     goto redraw;
  685.     } else if (eventPtr->type == ConfigureNotify) {
  686.     /*
  687.      * Must redraw after size changes, since layout could have changed
  688.      * and borders will need to be redrawn.
  689.      */
  690.  
  691.     goto redraw;
  692.     } else if (eventPtr->type == DestroyNotify) {
  693.     TkpDestroyMenuButton(mbPtr);
  694.     if (mbPtr->tkwin != NULL) {
  695.         mbPtr->tkwin = NULL;
  696.         Tcl_DeleteCommandFromToken(mbPtr->interp, mbPtr->widgetCmd);
  697.     }
  698.     if (mbPtr->flags & REDRAW_PENDING) {
  699.         Tcl_CancelIdleCall(TkpDisplayMenuButton, (ClientData) mbPtr);
  700.     }
  701.     Tcl_EventuallyFree((ClientData) mbPtr, DestroyMenuButton);
  702.     } else if (eventPtr->type == FocusIn) {
  703.     if (eventPtr->xfocus.detail != NotifyInferior) {
  704.         mbPtr->flags |= GOT_FOCUS;
  705.         if (mbPtr->highlightWidth > 0) {
  706.         goto redraw;
  707.         }
  708.     }
  709.     } else if (eventPtr->type == FocusOut) {
  710.     if (eventPtr->xfocus.detail != NotifyInferior) {
  711.         mbPtr->flags &= ~GOT_FOCUS;
  712.         if (mbPtr->highlightWidth > 0) {
  713.         goto redraw;
  714.         }
  715.     }
  716.     }
  717.     return;
  718.  
  719.     redraw:
  720.     if ((mbPtr->tkwin != NULL) && !(mbPtr->flags & REDRAW_PENDING)) {
  721.     Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
  722.     mbPtr->flags |= REDRAW_PENDING;
  723.     }
  724. }
  725.  
  726. /*
  727.  *----------------------------------------------------------------------
  728.  *
  729.  * MenuButtonCmdDeletedProc --
  730.  *
  731.  *    This procedure is invoked when a widget command is deleted.  If
  732.  *    the widget isn't already in the process of being destroyed,
  733.  *    this command destroys it.
  734.  *
  735.  * Results:
  736.  *    None.
  737.  *
  738.  * Side effects:
  739.  *    The widget is destroyed.
  740.  *
  741.  *----------------------------------------------------------------------
  742.  */
  743.  
  744. static void
  745. MenuButtonCmdDeletedProc(clientData)
  746.     ClientData clientData;    /* Pointer to widget record for widget. */
  747. {
  748.     TkMenuButton *mbPtr = (TkMenuButton *) clientData;
  749.     Tk_Window tkwin = mbPtr->tkwin;
  750.  
  751.     /*
  752.      * This procedure could be invoked either because the window was
  753.      * destroyed and the command was then deleted (in which case tkwin
  754.      * is NULL) or because the command was deleted, and then this procedure
  755.      * destroys the widget.
  756.      */
  757.  
  758.     if (tkwin != NULL) {
  759.     mbPtr->tkwin = NULL;
  760.     Tk_DestroyWindow(tkwin);
  761.     }
  762. }
  763.  
  764. /*
  765.  *--------------------------------------------------------------
  766.  *
  767.  * MenuButtonTextVarProc --
  768.  *
  769.  *    This procedure is invoked when someone changes the variable
  770.  *    whose contents are to be displayed in a menu button.
  771.  *
  772.  * Results:
  773.  *    NULL is always returned.
  774.  *
  775.  * Side effects:
  776.  *    The text displayed in the menu button will change to match the
  777.  *    variable.
  778.  *
  779.  *--------------------------------------------------------------
  780.  */
  781.  
  782.     /* ARGSUSED */
  783. static char *
  784. MenuButtonTextVarProc(clientData, interp, name1, name2, flags)
  785.     ClientData clientData;    /* Information about button. */
  786.     Tcl_Interp *interp;        /* Interpreter containing variable. */
  787.     char *name1;        /* Name of variable. */
  788.     char *name2;        /* Second part of variable name. */
  789.     int flags;            /* Information about what happened. */
  790. {
  791.     register TkMenuButton *mbPtr = (TkMenuButton *) clientData;
  792.     char *value;
  793.  
  794.     /*
  795.      * If the variable is unset, then immediately recreate it unless
  796.      * the whole interpreter is going away.
  797.      */
  798.  
  799.     if (flags & TCL_TRACE_UNSETS) {
  800.     if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
  801.         Tcl_SetVar(interp, mbPtr->textVarName, mbPtr->text,
  802.             TCL_GLOBAL_ONLY);
  803.         Tcl_TraceVar(interp, mbPtr->textVarName,
  804.             TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  805.             MenuButtonTextVarProc, clientData);
  806.     }
  807.     return (char *) NULL;
  808.     }
  809.  
  810.     value = Tcl_GetVar(interp, mbPtr->textVarName, TCL_GLOBAL_ONLY);
  811.     if (value == NULL) {
  812.     value = "";
  813.     }
  814.     if (mbPtr->text != NULL) {
  815.     ckfree(mbPtr->text);
  816.     }
  817.     mbPtr->text = (char *) ckalloc((unsigned) (strlen(value) + 1));
  818.     strcpy(mbPtr->text, value);
  819.     TkpComputeMenuButtonGeometry(mbPtr);
  820.  
  821.     if ((mbPtr->tkwin != NULL) && Tk_IsMapped(mbPtr->tkwin)
  822.         && !(mbPtr->flags & REDRAW_PENDING)) {
  823.     Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
  824.     mbPtr->flags |= REDRAW_PENDING;
  825.     }
  826.     return (char *) NULL;
  827. }
  828.  
  829. /*
  830.  *----------------------------------------------------------------------
  831.  *
  832.  * MenuButtonImageProc --
  833.  *
  834.  *    This procedure is invoked by the image code whenever the manager
  835.  *    for an image does something that affects the size of contents
  836.  *    of an image displayed in a button.
  837.  *
  838.  * Results:
  839.  *    None.
  840.  *
  841.  * Side effects:
  842.  *    Arranges for the button to get redisplayed.
  843.  *
  844.  *----------------------------------------------------------------------
  845.  */
  846.  
  847. static void
  848. MenuButtonImageProc(clientData, x, y, width, height, imgWidth, imgHeight)
  849.     ClientData clientData;        /* Pointer to widget record. */
  850.     int x, y;                /* Upper left pixel (within image)
  851.                      * that must be redisplayed. */
  852.     int width, height;            /* Dimensions of area to redisplay
  853.                      * (may be <= 0). */
  854.     int imgWidth, imgHeight;        /* New dimensions of image. */
  855. {
  856.     register TkMenuButton *mbPtr = (TkMenuButton *) clientData;
  857.  
  858.     if (mbPtr->tkwin != NULL) {
  859.     TkpComputeMenuButtonGeometry(mbPtr);
  860.     if (Tk_IsMapped(mbPtr->tkwin) && !(mbPtr->flags & REDRAW_PENDING)) {
  861.         Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
  862.         mbPtr->flags |= REDRAW_PENDING;
  863.     }
  864.     }
  865. }
  866.